/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.wuhenzhizao.titlebar.widget;

import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import org.wuhenzhizao.library.ResourceTable;

/**
 * CustomTitleBar
 *
 * @since 2021-06-08
 */
public class CustomTitleBar extends ComponentContainer {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.DEBUG, 0, "TAG");
    private DirectionalLayout leftBankDil;
    private Button leftBankButton;

    /**
     * CustomTitleBar
     *
     * @param context context
     */
    public CustomTitleBar(Context context) {
        super(context);
    }

    /**
     * CustomTitleBar
     *
     * @param context context
     * @param attrSet attrSet
     */
    public CustomTitleBar(Context context, AttrSet attrSet) {
        super(context, attrSet);
        Component component = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_layout_titlebar, null, false);
        leftBankDil = (DirectionalLayout) component.findComponentById(ResourceTable.Id_left_bank_dil);
        leftBankButton = (Button) component.findComponentById(ResourceTable.Id_left_bank_button);
        Text leftBankText = (Text) component.findComponentById(ResourceTable.Id_left_bank_text);
        DirectionalLayout leftScanDil = (DirectionalLayout) component.findComponentById(ResourceTable.Id_left_Scan_dil);
        Button leftScanButton = (Button) component.findComponentById(ResourceTable.Id_left_Scan_button);
        Text leftScanText = (Text) component.findComponentById(ResourceTable.Id_left_Scan_text);
        Button centreTitleButtonText = (Button) component.findComponentById(ResourceTable.Id_centre_title_ButtonText);
        Text centreSubText = (Text) component.findComponentById(ResourceTable.Id_centre_sub_text);
        Image centreProgressBar = (Image) component.findComponentById(ResourceTable.Id_centre_progress_img);
        Text pmdText = (Text) component.findComponentById(ResourceTable.Id_centre_pmd_text);
        DependentLayout contreSearchDel = (DependentLayout) component
                .findComponentById(ResourceTable.Id_centre_Search_del);
        AnimatorProperty animatorProperty = centreProgressBar.createAnimatorProperty();
        animatorProperty.rotate(360).setDuration(2000).setLoopedCount(10000);
        centreProgressBar.setBindStateChangedListener(new Component.BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                animatorProperty.start();
            }
            @Override
            public void onComponentUnboundFromWindow(Component component) {
                animatorProperty.stop();
            }
        });
        DirectionalLayout rightDil = (DirectionalLayout) component.findComponentById(ResourceTable.Id_right_del);
        Button rightZoom = (Button) component.findComponentById(ResourceTable.Id_right_Zoom);
        Text rightTextQd = (Text) component.findComponentById(ResourceTable.Id_right_text_qd);
        DirectionalLayout rightMessageDil =
                (DirectionalLayout) component.findComponentById(ResourceTable.Id_right_message_Dil);
        Button rightMessageImg = (Button) component.findComponentById(ResourceTable.Id_right_message_img);
        Button rightMessageYuan = (Button) component.findComponentById(ResourceTable.Id_right_message_yuan);
        Text rightMessageText = (Text) component.findComponentById(ResourceTable.Id_right_message_text);
        intiListeners();
        addComponent(component);
        leftBankDil.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                context.terminateAbility();
            }
        });
        leftBankButton.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                context.terminateAbility();
            }
        });
        if (attrSet.getAttr("bg_color").isPresent()) {
            component.setBackground(attrSet.getAttr("bg_color").get().getElement());
        } else {
            component.setBackground(getBackgroundElement());
        }
        if (attrSet.getAttr("left_bank_button").get().getBoolValue()) {
            leftBankButton.setVisibility(VISIBLE);
        } else {
            leftBankButton.setVisibility(HIDE);
        }
        if (attrSet.getAttr("left_bank_icon_text").get().getBoolValue()) {
            leftBankText.setVisibility(VISIBLE);
        } else {
            leftBankText.setVisibility(HIDE);
        }
        if (attrSet.getAttr("left_scan_dil").get().getBoolValue()) {
            leftScanDil.setVisibility(VISIBLE);
        } else {
            leftScanDil.setVisibility(HIDE);
        }
        if (attrSet.getAttr("left_bank_icon").isPresent()) {
            leftBankButton.setAroundElements(attrSet.getAttr("left_bank_icon").get()
                    .getElement(), null, null, null);
        }
        if (attrSet.getAttr("left_scan_icon").isPresent()) {
            leftScanButton.setAroundElements(attrSet.getAttr("left_scan_icon").get()
                    .getElement(), null, null, null);
        }
        if (attrSet.getAttr("left_bank_text").isPresent()) {
            leftBankText.setText(attrSet.getAttr("left_bank_text").get().getStringValue());
        } else {
            leftBankText.setText("");
        }
        if (attrSet.getAttr("left_scan_icon_text").isPresent()) {
            leftScanText.setText(attrSet.getAttr("left_scan_icon_text").get().getStringValue());
        } else {
            leftScanText.setText("");
        }
        if (attrSet.getAttr("left_bank_text_size").isPresent()) {
            leftBankText.setTextSize(attrSet.getAttr("left_bank_text_size").get()
                    .getIntegerValue(), Text.TextSizeType.FP);
        }
        if (attrSet.getAttr("centre_title_buttontext").isPresent()) {
            centreTitleButtonText.setText(attrSet.getAttr("centre_title_buttontext").get().getStringValue());
        }
        if (attrSet.getAttr("centre_sub_text").isPresent()) {
            centreSubText.setText(attrSet.getAttr("centre_sub_text").get().getStringValue());
        }
        if (attrSet.getAttr("centre_title_button_textsize").isPresent()) {
            centreTitleButtonText.setTextSize(attrSet.getAttr("centre_title_button_textsize")
                    .get().getIntegerValue(), Text.TextSizeType.FP);
        }
        if (attrSet.getAttr("center_title_button_textcolor").isPresent()) {
            centreTitleButtonText.setTextColor(attrSet.getAttr("center_title_button_textcolor")
                    .get().getColorValue());
        } else {
            centreTitleButtonText.setTextColor(Color.BLACK);
        }
        if (attrSet.getAttr("centre_title_button_icon").isPresent()) {
            centreTitleButtonText.setAroundElements(attrSet.getAttr("centre_title_button_icon")
                    .get().getElement(), null, null, null);
        }
        if (attrSet.getAttr("contre_search_del").get().getBoolValue()) {
            contreSearchDel.setVisibility(VISIBLE);
        } else {
            contreSearchDel.setVisibility(HIDE);
        }
        if (attrSet.getAttr("centre_pmd").get().getBoolValue()) {
            pmdText.setVisibility(VISIBLE);
        } else {
            pmdText.setVisibility(HIDE);
        }
        pmdText.setTruncationMode(Text.TruncationMode.AUTO_SCROLLING);
        pmdText.setAutoScrollingCount(Text.AUTO_SCROLLING_FOREVER);
        pmdText.startAutoScrolling();
        if (attrSet.getAttr("centre_progress_img").get().getBoolValue()) {
            centreProgressBar.setVisibility(VISIBLE);
        } else {
            centreProgressBar.setVisibility(HIDE);
        }
        if (attrSet.getAttr("right_dil").get().getBoolValue()) {
            rightDil.setVisibility(VISIBLE);
        } else {
            rightDil.setVisibility(HIDE);
        }
        if (attrSet.getAttr("right_text").isPresent()) {
            rightTextQd.setText(attrSet.getAttr("right_text").get().getStringValue());
        } else {
            rightTextQd.setText("");
        }
        if (attrSet.getAttr("right_zoom_icon").isPresent()) {
            rightZoom.setAroundElements(attrSet.getAttr("right_zoom_icon").get()
                    .getElement(), null, null, null);
        }
        if (attrSet.getAttr("right_message_dil").get().getBoolValue()) {
            rightMessageDil.setVisibility(VISIBLE);
        } else {
            rightMessageDil.setVisibility(HIDE);
        }
        if (attrSet.getAttr("right_message_img").isPresent()) {
            rightMessageImg.setAroundElements(attrSet.getAttr("right_message_img").get()
                    .getElement(), null, null, null);
        }
        if (attrSet.getAttr("right_message_img_yuan").isPresent()) {
            rightMessageYuan.setAroundElements(attrSet.getAttr("right_message_img_yuan").get()
                    .getElement(), null, null, null);
        }
        if (attrSet.getAttr("right_message_img_yuan_text").isPresent()) {
            rightMessageYuan.setText(attrSet.getAttr("right_message_img_yuan_text").get().getStringValue());
        }
        if (attrSet.getAttr("right_message_text").isPresent()) {
            rightMessageText.setText(attrSet.getAttr("right_message_text").get().getStringValue());
        }
    }

    private void intiListeners() {
    }
}
